//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;

using CommunityServer.Components;
using CommunityServer.Discussions.Components;

namespace CommunityServer.Discussions.Controls 
{

    [
    ParseChildren(true)
    ]
    public class JumpDropDownList : DropDownList 
    {


        public JumpDropDownList() 
        {
        	CSContext cntx = CSContext.Current;
        	// Set up some default property values
            //
            AutoPostBack = true;
            SelectedIndexChanged += new System.EventHandler(Location_Changed);

            // head of the drop down
            //
            Items.Add(new ListItem( ResourceManager.GetString("Navigation_JumpDropDownList_Title"), "" ));
            Items.Add(new ListItem( ResourceManager.GetString("Navigation_JumpDropDownList_Seperator") ));

            // standard "home" links
            //
            Items.Add(new ListItem( ResourceManager.GetString("Navigation_JumpDropDownList_Home"), Globals.GetSiteUrls().ForumsHome));
            Items.Add(new ListItem( ResourceManager.GetString("Navigation_JumpDropDownList_Indent1") + ResourceManager.GetString("Navigation_JumpDropDownList_Search"), Globals.GetSiteUrls().Search));
            Items.Add(new ListItem( ResourceManager.GetString("Navigation_JumpDropDownList_Indent1") + ResourceManager.GetString("ViewActiveThreads_Title"), ForumUrls.Instance().PostsActive));
            Items.Add(new ListItem( ResourceManager.GetString("Navigation_JumpDropDownList_Indent1") + ResourceManager.GetString("ViewUnansweredThreads_Title"), ForumUrls.Instance().PostsUnanswered));
			if(!cntx.User.IsAnonymous)
				Items.Add(new ListItem( ResourceManager.GetString("Navigation_JumpDropDownList_Indent1") + ResourceManager.GetString("ViewNotReadThreads_Title"), ForumUrls.Instance().PostsNotRead));

            // User Options to display, based if the user is signed in or not.
            //
            if (cntx.User.IsForumAdministrator || cntx.User.IsModerator) 
            {
                Items.Add(new ListItem( ResourceManager.GetString("Navigation_JumpDropDownList_Seperator") ));
                Items.Add(new ListItem( ResourceManager.GetString("Navigation_JumpDropDownList_AdminOptions") ));
                if ( cntx.User.IsForumAdministrator  )
                    Items.Add(new ListItem( ResourceManager.GetString("Navigation_JumpDropDownList_Indent1") + ResourceManager.GetString("Navigation_JumpDropDownList_AdminHome"), Globals.GetSiteUrls().ControlPanel ));
                Items.Add(new ListItem( ResourceManager.GetString("Navigation_JumpDropDownList_Indent1") + ResourceManager.GetString("Navigation_JumpDropDownList_ModeratorHome"), Globals.GetSiteUrls().ModerationHome ));
            }            

            // seperator
            //
            Items.Add(new ListItem( ResourceManager.GetString("Navigation_JumpDropDownList_Seperator") ));

            // User Options to display, based if the user is signed in or not.
            //
            if (!cntx.User.IsAnonymous) 
            {
                Items.Add(new ListItem( ResourceManager.GetString("Navigation_JumpDropDownList_UserOptions") ));
                Items.Add(new ListItem( ResourceManager.GetString("Navigation_JumpDropDownList_Indent1") + ResourceManager.GetString("Navigation_JumpDropDownList_Profile"), Globals.GetSiteUrls().UserEditProfile ));
                Items.Add(new ListItem( ResourceManager.GetString("Navigation_JumpDropDownList_Indent1") + ResourceManager.GetString("Navigation_JumpDropDownList_PrivateMessages"), Globals.GetSiteUrls().UserPrivateMessages ));
                Items.Add(new ListItem( ResourceManager.GetString("Navigation_JumpDropDownList_Indent1") + ResourceManager.GetString("Navigation_JumpDropDownList_MyThreads"), Globals.GetSiteUrls().UserMyForums ));
            } 
            else 
            {
                Items.Add(new ListItem( ResourceManager.GetString("Navigation_JumpDropDownList_UserOptions") ));
                Items.Add(new ListItem( ResourceManager.GetString("Navigation_JumpDropDownList_Indent1") + ResourceManager.GetString("Navigation_JumpDropDownList_Login"), Globals.GetSiteUrls().Login ));
                Items.Add(new ListItem( ResourceManager.GetString("Navigation_JumpDropDownList_Indent1") + ResourceManager.GetString("Navigation_JumpDropDownList_CreateAccount"), Globals.GetSiteUrls().UserRegister ));
                Items.Add(new ListItem( ResourceManager.GetString("Navigation_JumpDropDownList_Indent1") + ResourceManager.GetString("Navigation_JumpDropDownList_ForgotPassword"), Globals.GetSiteUrls().UserForgotPassword ));
            }

            // seperator
            //
            Items.Add(new ListItem( ResourceManager.GetString("Navigation_JumpDropDownList_Seperator") ));

            // Get all forum groups and the remaining items are displayed
            // as ForumGroups and Forums.
            //
            ArrayList forumGroups = ForumGroups.GetForumGroups(true);

            foreach (Group group in forumGroups) 
            {
				//lazy load sections
				if(!group.HasSections)
					group.Sections = Forums.GetForumsByForumGroupID(group.GroupID);

				//Only add if we have sections. We do not display groups that have no sections
				if(group.HasSections)
				{

					// Add the forum group
					//
					Items.Add(new ListItem(group.Name, "g-" + group.GroupID));


					// Add all forums recursively.
					//
					RecursiveAddForum (0, group.Sections);

					// Add the forum group
					//
					Items.Add(new ListItem( ResourceManager.GetString("Navigation_JumpDropDownList_Seperator") ));
				}
            }
        }

        void RecursiveAddForum (int depth, ArrayList forums) 
        {
            
            foreach (Forum forum in forums) 
            {
                // We only go 3 deep
                //

                switch (depth) 
                {
                    case 0:

                        Items.Add(new ListItem(ResourceManager.GetString("Navigation_JumpDropDownList_Indent1") + forum.Name, "f-" + forum.SectionID.ToString()));
						if (forum.Sections.Count > 0)
						{
							// Sort subforums by name
							forum.Sections.Sort(new SectionSorter(SectionSortBy.Name));

							RecursiveAddForum((depth + 1), forum.Sections);
						}
                        break;

                    case 1:
                        Items.Add(new ListItem(ResourceManager.GetString("Navigation_JumpDropDownList_Indent2") + forum.Name, "f-" + forum.SectionID.ToString()));
						if (forum.Sections.Count > 0)
						{
							// Sort subforums by name
							forum.Sections.Sort(new SectionSorter(SectionSortBy.Name));

							RecursiveAddForum((depth + 1), forum.Sections);
						}
                        break;

                    case 2:
                        Items.Add(new ListItem(ResourceManager.GetString("Navigation_JumpDropDownList_Indent3") + forum.Name, "f-" + forum.SectionID.ToString()));
                        if (forum.Sections.Count > 0)
                            RecursiveAddForum((depth + 1), forum.Sections);
                        break;

                    default:
                        return;

                }
            }
        }


        // *********************************************************************
        //  Location_Changed
        //
        /// <summary>
        /// User wants to jump to a new location
        /// </summary>
        /// 
        // ********************************************************************/ 
        private void Location_Changed(Object sender, EventArgs e) 
        {

            DropDownList jumpLocation = (DropDownList) sender;
            string jumpValue = jumpLocation.SelectedItem.Value;

            if (jumpValue.StartsWith("/")) 
            {
                Page.Response.Redirect(jumpValue);
            } 
            else if (jumpValue.StartsWith("g")) 
            {
                int forumGroupId = 0;
                forumGroupId = Convert.ToInt32(jumpValue.Substring(jumpValue.IndexOf("-") + 1));
                Page.Response.Redirect(Globals.GetSiteUrls().ForumGroup( forumGroupId)  );
            } 
            else if (jumpValue.StartsWith("f")) 
            {
                int forumId = 0;
                forumId = Convert.ToInt32(jumpValue.Substring(jumpValue.IndexOf("-") + 1));
                Page.Response.Redirect(ForumUrls.Instance().Forum(forumId) );
            } 
            else 
            {
                Page.Response.Redirect(Globals.GetSiteUrls().Home);
            }

            // End the response
            Page.Response.End();
        }


        // *********************************************************************
        //  DisplayText
        //
        /// <summary>
        /// Text preceding the drop down list of options
        /// </summary>
        /// 
        // ********************************************************************/ 
        public virtual String DisplayText 
        {
            get 
            {
                Object state = ViewState["DisplayText"];
                if ( state != null ) 
                {
                    return (String)state;
                }
                return ResourceManager.GetString("Navigation_JumpDropDownList_displayText");
            }
            set 
            {
                ViewState["DisplayText"] = value;
            }
        }

    }
}
